package com.cy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.common.ResultResponse;
import com.cy.entity.SchoolInfo;
import com.cy.entity.Statistics;
import com.cy.mapper.SchoolInfoMapper;
import com.cy.mapper.StatisticsMapper;
import com.cy.service.ISchoolInfoService;
import com.cy.service.IStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Aleven
 * @since 2024-12-21
 */
@Service
public class SchoolInfoServiceImpl extends ServiceImpl<SchoolInfoMapper, SchoolInfo> implements ISchoolInfoService {

    @Autowired
    private SchoolInfoMapper schoolInfoMapper;
    @Override
    public ResultResponse selectPage(Integer page, Integer limit) throws Exception {
        if (page==null||limit==null){
            return ResultResponse.error();
        }
        IPage<SchoolInfo> selectPage = new Page<>(page, limit);
        selectPage = schoolInfoMapper.selectPage(selectPage, null);
        return ResultResponse.success(selectPage);
    }

    @Override
    public ResultResponse addInfo(SchoolInfo schoolInfo) throws Exception {
        if (schoolInfo==null){
            return ResultResponse.error();
        }
        QueryWrapper<SchoolInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("UserID",schoolInfo.getUserID());
        SchoolInfo one = schoolInfoMapper.selectOne(queryWrapper);
        if (one!=null) {
            return ResultResponse.exist();
        }
        Integer i = schoolInfoMapper.insertSchoolInfo(schoolInfo);
        return i>0?ResultResponse.success():ResultResponse.fail();
    }

    @Override
    public ResultResponse updateInfo(SchoolInfo schoolInfo) throws Exception {
        if (schoolInfo==null){
            return ResultResponse.error();
        }
        Integer i = schoolInfoMapper.updateSchoolInfo(schoolInfo);
        return i>0?ResultResponse.success():ResultResponse.fail();
    }

    @Override
    public ResultResponse deleteInfo(Integer Id) throws Exception {
        if (Id==null){
            return ResultResponse.error();
        }
        SchoolInfo info = schoolInfoMapper.selectById(Id);
        if (info==null){
            return ResultResponse.notFound();
        }
        Integer i = schoolInfoMapper.deleteSchoolInfo(Id);
        return i>0?ResultResponse.success():ResultResponse.fail();
    }
}